package cn.wu.reggie.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.wu.reggie.common.Page;
import cn.wu.reggie.common.R;
import cn.wu.reggie.dto.DishDto;
import cn.wu.reggie.entity.Category;
import cn.wu.reggie.entity.Dish;
import cn.wu.reggie.entity.DishFlavor;
import cn.wu.reggie.mapper.DishMapper;
import cn.wu.reggie.query.DishQuery;
import cn.wu.reggie.service.ICategoryService;
import cn.wu.reggie.service.IDishDtoService;
import cn.wu.reggie.service.IDishFlavorService;
import cn.wu.reggie.service.IDishService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements IDishService {

    @Autowired
    private IDishFlavorService dishFlavorService;

    @Autowired
    private ICategoryService categoryService;


    @Override
    @Transactional
    public R saveDishWithFlavor(DishDto dishDto) {
        // 1 保存菜品 设置排序 时间
        dishDto.setSort(0);
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(dishDto.getCreateTime());
        // 保存  疑问这里需不需要获取自增id
        save(dishDto);


        // 2 保存菜品口味  遍历赋值口味对应的菜品dish_id 和 用户id时间
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(f -> {
            f.setDishId(dishDto.getId()); // 疑问这里需不需要获取自增id

            f.setCreateTime(dishDto.getCreateTime());
            f.setUpdateTime(dishDto.getUpdateTime());

            f.setUpdateUser(dishDto.getUpdateUser());
            f.setCreateUser(dishDto.getCreateUser());
        });

        // 批量保存口味对象
        dishFlavorService.saveBatch(flavors);

        return R.success(null);

    }


/*    @Override
    public Page<Dish> findByPage(DishQuery qo) {

        //=====================================================================
        // 1 查询菜品分类列表 把list转成map  分类id 分类名称
        QueryWrapper<Category> categoryWrapper = new QueryWrapper<>();
        categoryWrapper.eq("type", 1);
        List<Category> categoryList = categoryService.getBaseMapper().selectList(categoryWrapper);
        Map<Long, String> categoryMap = new HashMap<>();
        categoryList.forEach(c -> {
            categoryMap.put(c.getId(), c.getName());
        });
        QueryWrapper<Dish> dishWrapper = new QueryWrapper<>();
        List<Dish> dishList = getBaseMapper().selectList(dishWrapper);
        // 3 新建一个DtoList 遍历查询的结合 将dish封装成dishDto 且给dto设置分类名称 将dto放入dtoList中
        List<DishDto> dtoList = new ArrayList<>();
        if (CollUtil.isNotEmpty(dishList)) {
            dishList.forEach(dish -> {
                DishDto dto = new DishDto();
                // 拷贝 设置分类名称
                BeanUtils.copyProperties(dish, dto);
                dto.setCategoryName(categoryMap.get(dish.getCategoryId()));
                dtoList.add(dto);
            });
        }
        // ===================================================================
        // 创建mybatis-plus page对象 设置分页参数
        Page<Dish> dishPage = new Page<>(qo.getPage(), qo.getPageSize());
        // 2 根据名称查询所有
//        QueryWrapper<Dish> dishWrapper = new QueryWrapper<>();
        dishWrapper.like(qo.getName() != null, "name", qo.getName());
         dishPage = getBaseMapper().selectPage(dishPage, dishWrapper);
        return dishPage;
    }*/


/*    @Override
    public Page<Dish> findByPage(DishQuery qo) {
        // 1 查询菜品分类列表 把list转成map  分类id 分类名称
        QueryWrapper<Category> categoryWrapper = new QueryWrapper<>();
        categoryWrapper.eq("type", 1);
        List<Category> categoryList = categoryService.getBaseMapper().selectList(categoryWrapper);
        Map<Long, String> categoryMap = new HashMap<>();
        categoryList.forEach(c -> {
            categoryMap.put(c.getId(), c.getName());
        });
        // 2 根据名称查询所有
        QueryWrapper<Dish> dishWrapper = new QueryWrapper<>();
        dishWrapper.like(qo.getName()!=null,"name", qo.getName());
//        List<Dish> dishList = getBaseMapper().selectList(dishWrapper);

*//*        // 3 新建一个DtoList 遍历查询的结合 将dish封装成dishDto 且给dto设置分类名称 将dto放入dtoList中
        List<DishDto> dtoList = new ArrayList<>();
        if (CollUtil.isNotEmpty(dishList)) {
            dishList.forEach(dish -> {
                DishDto dto = new DishDto();
                // 拷贝 设置分类名称
                BeanUtils.copyProperties(dish, dto);
                dto.setCategoryName(categoryMap.get(dish.getCategoryId()));
                dtoList.add(dto);
            });
        }*//*
        // 4 设置分页参数
        Page<Dish> dishPage = new Page<>(qo.getPage(), qo.getPageSize());
        return page(dishPage, dishWrapper);
    }*/

/*    @Override
    public R findByPage(DishQuery qo) {
        // 1 查询菜品分类列表 把list转成map  分类id 分类名称
        QueryWrapper<Category> categoryWrapper = new QueryWrapper<>();
        categoryWrapper.eq("type",1);
        List<Category> categoryList = categoryService.getBaseMapper().selectList(categoryWrapper);
        Map<Long, String> categoryMap = new HashMap<>();
        categoryList.forEach(c->{
            categoryMap.put(c.getId(),c.getName());
        });

        // 设置分页参数  放在这里可以
        PageHelper.startPage(qo.getPage(),qo.getPageSize());

        // 2 根据名称查询所有
        QueryWrapper<Dish> dishWrapper = new QueryWrapper<>();
        dishWrapper.like(qo.getName()!=null,"name", qo.getName());
        List<Dish> dishList = getBaseMapper().selectList(dishWrapper);
        // 4 新建一个DtoList 遍历查询的结合 将dish封装成dishDto 且给dto设置分类名称 将dto放入dtoList中
        List<DishDto> dtoList = new ArrayList<>();
        if (CollUtil.isNotEmpty(dishList)) {
            dishList.forEach(dish -> {
                DishDto dto = new DishDto();
                // 拷贝 设置分类名称
                BeanUtils.copyProperties(dish,dto);
                dto.setCategoryName(categoryMap.get(dish.getCategoryId()));
                dtoList.add(dto);
            });
        }
        // 5 返回自定义分页参数

        PageInfo<DishDto> pageInfo = new PageInfo<>(dtoList);
        List<DishDto> list = pageInfo.getList();

        Page<DishDto> dishDtoPage = new Page<>(qo.getPage(),qo.getPageSize(),list.size());
        dishDtoPage.setRecords(list);
//        page.setSize(qo.getPageSize());
//        page.setCurrent(qo.getPage());
        return R.success(dishDtoPage);
    }*/

    @Override
    public R findByPage(DishQuery qo) {
        // 1 pageHelper 分页查询
        // 1.1 设置分页参数
        PageHelper.startPage(qo.getPage(),qo.getPageSize());
        // 1.2 根据名称查询所有菜品
        QueryWrapper<Dish> dishWrapper = new QueryWrapper<>();
        dishWrapper.like(qo.getName()!=null,"name", qo.getName());
        List<Dish> dishList = getBaseMapper().selectList(dishWrapper);
        // 1.3 将查询的集合封装成PageInfo
        PageInfo<Dish> pageInfo = new PageInfo<>(dishList);
        // 2 封装dto信息
        // 2.1查询菜品分类列表 把list转成map  分类id 分类名称
        QueryWrapper<Category> categoryWrapper = new QueryWrapper<>();
        categoryWrapper.eq("type",1);
        List<Category> categoryList = categoryService.getBaseMapper().selectList(categoryWrapper);
        Map<Long, String> categoryMap = new HashMap<>();
        categoryList.forEach(c->{
            categoryMap.put(c.getId(),c.getName());
        });
        // 2.2 新建一个DtoList 遍历查询的结合 将dish封装成dishDto 且给dto设置分类名称 将dto放入dtoList中
        List<DishDto> dtoList = new ArrayList<>();
        if (CollUtil.isNotEmpty(dishList)) {
            dishList.forEach(dish -> {
                DishDto dto = new DishDto();
                // 拷贝 设置分类名称
                BeanUtils.copyProperties(dish,dto);
                dto.setCategoryName(categoryMap.get(dish.getCategoryId()));
                dtoList.add(dto);
            });
        }
        // 3 返回自定义分页参数
        return R.success(new Page<>(dtoList,pageInfo.getTotal()));
    }




    @Override
    public R findById(Long id) {
        // 1 查询菜品基本信息
        Dish dish = getById(id);
        // 2 查询菜品口味信息
        List<DishFlavor> flavors = dishFlavorService.getBaseMapper().selectList(
                new QueryWrapper<DishFlavor>().eq("dish_id", id));
        // 3 将两个信息封装成dto对象返回
        DishDto dto = new DishDto();
        BeanUtils.copyProperties(dish, dto);
        dto.setFlavors(flavors);
        return R.success(dto);
    }

    @Override
    @Transactional
    public R updateDishWithFlavor(DishDto dishDto) {
        // 1 给dish修改时间 更新dish
        dishDto.setUpdateTime(LocalDateTime.now());
        updateById(dishDto);

        // 2 根据dish先删除口味中的所有信息
        dishFlavorService.remove(
                new QueryWrapper<DishFlavor>().eq("dish_id", dishDto.getId()));

        // 3 获取页面传递过来的口味对象 批量保存
        List<DishFlavor> flavors = dishDto.getFlavors();
        if (CollUtil.isNotEmpty(flavors)) {
            flavors.forEach(f -> {
                // 给口味对象设置 菜品id 时间 修改人 创建人
                f.setDishId(dishDto.getId());

                f.setCreateTime(dishDto.getCreateTime());
                f.setUpdateTime(dishDto.getUpdateTime());

                f.setCreateUser(dishDto.getCreateUser());
                f.setUpdateUser(dishDto.getUpdateUser());
            });

            // 保存所有的口味对象
            dishFlavorService.saveBatch(flavors);
        }
        return R.success(null);
    }
}
